/*
    8. Write a program in C to delete a node from the middle of Singly Linked List. 
    Test Data and Expected Output :

    Input the number of nodes : 3                                                                                
    Input data for node 1 : 2                                                                                    
    Input data for node 2 : 5                                                                                    
    Input data for node 3 : 8                                                                                    
                                                                                                                
    Data entered in the list are :                                                                               
    Data = 2                                                                                                     
    Data = 5                                                                                                     
    Data = 8                                                                                                     
                                                                                                                
    Input the position of node to delete : 2                                                                     
                                                                                                                
    Deletion completed successfully.                                                                             
                                                                                                                
    The new list are  :                                                                                          
    Data = 2                                                                                                     
    Data = 8  
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node* List;
typedef struct node{
    int x;
    List next;
} Node;

List createList(int n)
{
    List newNode = NULL;
    List curNode = NULL;
    List root = NULL;
    int i;
    for(i = 0; i < n; i++){
        newNode = (List)malloc(sizeof(Node));
        if(newNode == NULL){
            return NULL;
        }
        newNode->next = NULL;
        printf("Input data for node %d : ", i);
        scanf("%d", &(newNode->x));
        if(root == NULL){
            root = newNode;
            curNode = root;
        }else{
            curNode->next = newNode;
            curNode = newNode;
        }
    }
    return root;
}

List deleteMiddleNode(List root, int pos)
{
    List curNode = root;
    List preNode = root;
    int cnt = 0;
    if(root == NULL || pos < 2){
        return root;
    }
    while(curNode != NULL){
        cnt++;
        if(cnt == pos){
            break;
        }
        preNode = curNode;
        curNode = curNode->next;
    }
    preNode->next = curNode->next;
    free(curNode);

    return root;
}

void printList(List list)
{
    if(list == NULL){
        printf("The list is empty!\n");
        return;
    }
    while(list != NULL){
        printf("Data = %d\n", list->x);
        list = list->next;
    }    
}

int main(void)
{
    List root = NULL;
    int n, pos;
    printf("Input the number of nodes : ");
    scanf("%d", &n);
    root = createList(n);
    printf("\nData entered in the list are : \n");
    printList(root);
    printf("\nInput the position of node to delete : ");
    scanf("%d", &pos);
    root = deleteMiddleNode(root, pos);
    printf("\nDeletion completed successfully.");
    printf("\nThe new list are  : \n");
    printList(root);

    return 0;
}